#include "myTask.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "event_groups.h"
#include "queue.h"

#include "armaction.h"
#include "armeventctrl.h"

#include "armmotor.h"
#include "stepper.h"
#include "stepperctrl.h"
#include "usarttransmitter.h"
#include "cantransmitter.h"
#include "communication.h"
#include "switch.h"
#include "Air_control.h"
#include "RS7_ABSencoder.h"
#include "RGB_Belt.h"

#include "RelayMotor.h"
#include "servercomm.h"
#include "manage.h"

#include "bsp_timer.h"
#include "bsp_usart.h"
#include "bsp_gpio.h"
#include "crc8.h"
#include "string.h"
#include "bsp_adc.h"

#include "DroneCtrl.h"
#include "MasterAppReset.h"





extern QueueHandle_t Usart1Debug_Queue;
extern QueueHandle_t ServerCmd_Queue;	//串口2接收信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Can_Tx_Queue;		//CAN信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Usart3_Rx_Queue;	//串口3接收信息队列句柄，用于中断和任务通讯
extern QueueHandle_t Usart6_Rx_Queue;	//串口6信息队列句柄，用于中断和任务通讯

//LightLimit_task
void LightLimit_task(void *pvParameters)
{
	while(1)
	{
		ArmMotorLimitCtrl();     //光电检测
		if(!Get_ArmReset())
			reset2isp();
		
		
		hard_watchdog_feed();//硬件喂狗		
        vTaskDelay(10);
	}
}

//step_motor_task
void step_motor_task(void *pvParameters)
{
	while(1)
	{
	    ArmMotorCtrl();
        vTaskDelay(10);
	}
}

//Armevent_task
void Armevent_task(void *pvParameters)
{
	while(1)
	{
		ArmEventHandle();
		green_led_blink();
		vTaskDelay(10);
	}
}



T_RSKTCN01_DATA  s_rsktcn01_data;
T_WEATHER_DATA s_weather_data;
Air_control_DATA s_aircon;

uint8_t Send_flag = 0;
void comunication_task(void *pvParameters)
{
	uint8_t usart1queuebuf[USART1_RX_SIZE]      = {0};
	uint8_t serverqueuebuf[USART2_MAX_RECV_LEN] = {0};
	uint8_t usart3queuebuf[USART3_MAX_RECV_LEN] = {0};
	uint8_t usart6queuebuf[USART6_MAX_RECV_LEN] = {0};
	uint8_t server_uploda_cnt = 0;
	while(1)
	{
		if(xQueueReceive(Usart1Debug_Queue,usart1queuebuf,10)==pdTRUE){
	    #if (HANGAR_MODE == 1)
			Drones_Data_Pase(usart1queuebuf);
			UsartProcess(usart1queuebuf,USART1_RX_SIZE);//解析处理串口1调试数据
		#elif (HANGAR_MODE == 2)
			ServerCmdPrase(usart1queuebuf,USART1_RX_SIZE);//解析服务器数据
		#endif
		}
		
		if(xQueueReceive(ServerCmd_Queue,serverqueuebuf,10)==pdTRUE){
			SeriverToID(serverqueuebuf);
			Server_Param_Pase(serverqueuebuf);
			ServerCmdPrase(serverqueuebuf,USART2_MAX_RECV_LEN);//解析服务器数据
		}
		
		if(xQueueReceive(Usart3_Rx_Queue,usart3queuebuf,10)==pdTRUE){
			Weather_Fun.Weather_Prase(usart3queuebuf,USART3_MAX_RECV_LEN);//解析温湿度计数据
			Air_control_Fun.Air_control_Prase(usart3queuebuf,USART3_MAX_RECV_LEN);//解析空调控制器数据
			Rsktcn01_fun.RSKTCN01_Prase(usart3queuebuf,USART3_MAX_RECV_LEN);//解析气象站数据
			s_rsktcn01_data = Rsktcn01_fun.RSKTCN01_Get_Data();
			s_weather_data = Weather_Fun.Weather_Data_Get();
			s_aircon = Air_control_Fun.Air_control_Data_Get();
		}
		
		if(xQueueReceive(Usart6_Rx_Queue,usart6queuebuf,10)==pdTRUE){
			Rs7abs_fun.RS7ABS_Prase(usart6queuebuf,USART6_MAX_RECV_LEN);//解析绝对式编码器数据
			Rs7abs_fun.RS7ABS_Get_Abs();
		}
		
		if(++server_uploda_cnt>5){
			server_uploda_cnt = 0;
			Sensor_Data_Packed_Upload(s_rsktcn01_data,s_weather_data,s_aircon);//2HZ
		}
		if(Send_flag)
		{
			Send_flag = 0;
	    	Server_Param_Send();
		}
		vTaskDelay(100);
	}
}

uint8_t i_flog=1;
void sensor_task(void *pvParameters)
{
	static uint8_t rs_flag = 1,cnt = 1;
	RELAY1_ON();//OPEN brake
	RELAY7_ON();//OPEN FAN
	while(1)
	{	
		Relay_Ctrl();//继电器
		if(i_flog <= 3)
		{
			Air_control_Fun.Air_control_Data_Ask();//制冷空调
			i_flog++;
		}
		else if(i_flog > 3& i_flog <= 6)
		{
			Weather_Fun.Weather_Data_Ask();//气象站
			i_flog++;
		}
		else if(i_flog > 6)
		{
			Rsktcn01_fun.RSKTCN01_Data_Ask();//温湿度计
			(i_flog >= 9) ? (i_flog = 1) : (i_flog++);
		}
		if(rs_flag == 1){
			Rs7abs_fun.RS7ABS_Data_Ask(1);//绝对式编码器1
			rs_flag = 2;
		}
		else if(rs_flag == 2){
			Rs7abs_fun.RS7ABS_Data_Ask(2);//绝对式编码器2
			rs_flag = 1;
		}
		(cnt > 5) ? (SwitchTest(),cnt = 0) : (cnt++);
		Usart2_Send();
		red_led_blink();
		vTaskDelay(100);
	}
}


uint8_t flag = 0;

void manage_task(void *pvParameters)
{
	static uint8_t OffLine_Time = 0,flag_time = 0;
	while(1)
	{
		RGB_Manage();
		
		if(flag_time >= 60)
			(OffLine_Time > 40) ? (OffLine_Manage(),OffLine_Time = 0):(OffLine_Time++);
		else 
			flag_time++;
	    
		
		Reset_Motor();
		Hangar_State_Manage();
		Flash_Manage();
		Other_Manage();
		if(flag)
		{
		  RELAY2_ON();
		}
		else
		{
			 RELAY2_OFF();
		}

		vTaskDelay(50);	
	}
}



